package com.teamclub.app.sservices

import io.ebean.{EbeanServer, Query}
import com.fasterxml.jackson.databind.JsonNode
import com.teamclub.app.sforms.AppSubjectUserForm
import com.teamclub.domain.system.AppSubjectUser
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.{F, Json, MD5}
import com.teamclub.util.page.Pager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
  * Created by ilkkzm on 17-4-27.
  */
@Service
class LoginService {
  @Autowired
  private val server:EbeanServer = null

  def getList(form: AppSubjectUserForm): F.Either[JsonNode, ErrorCode] = {
    val query = server.find(classOf[AppSubjectUser])
    expression(query, form)
    F.Either.Left(Pager.genePagerJson(query, form))
  }

  def exits(form: AppSubjectUserForm): F.Either[JsonNode, ErrorCode] = {
    var userName = ""
    var organNo = ""
    if(form.userName.indexOf("@") == -1) {
      userName = form.userName
      organNo = form.userName
    } else {
      userName = form.userName.substring(0, form.userName.indexOf("@"))
      organNo = form.userName.substring(form.userName.indexOf("@") + 1)
    }
    val single = server.find(classOf[AppSubjectUser]).where()
      .eq("userName", userName)
      .eq("userPassword", form.userPassword)
      .eq("organNo", organNo)
      .findOne()
    Option.apply(single) match {
      case Some(v) => F.Either.Left(Json.toJson(single))
      case None => F.Either.Right(ErrorCode.apply(555, "帐号密码错误"));
    }
  }

  def expression(query: Query[AppSubjectUser], form: AppSubjectUserForm): Query[AppSubjectUser] = {
    query
  }
}